package com.cy.controller;

import com.cy.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AuthController {
    @GetMapping("/auth/info")
    public Map<String, Object> getAuthentication(String token) {
        Claims claims = JwtUtils.getClaimsFromToken(token);
        String username = (String) claims.get("username");
        boolean expired = claims.getExpiration().before(new Date());
        List<String> authorites = (List<String>) claims.get("authorities");
        HashMap<String, Object> map = new HashMap<>();
        map.put("expired",expired);
        map.put("username",username);
        map.put("authorities",authorites);
        return map;
    }
}
